package com.lanou.myim.presenter;

import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;

import com.easemob.EMCallBack;
import com.easemob.chat.EMChatManager;
import com.easemob.chat.EMMessage;
import com.lanou.myim.application.BaseApplication;
import com.lanou.myim.entity.MsgEty;
import com.lanou.myim.entity.SendTexCallBacktEty;
import com.lanou.myim.model.PrivateChatModel;
import com.lanou.myim.model.PrivateChatModelInterface;
import com.lanou.myim.ui.activity.PrivateChatActivityInterface;
import com.lanou.myim.util.BarcodeFormat;

import java.util.List;

import de.greenrobot.event.EventBus;

/**
 * Created by dllo on 16/3/15.
 */
public class PrivateChatPresenter implements PrivateChatPresenterInterface {
    private PrivateChatActivityInterface privateChatActivity;
    private PrivateChatModelInterface privateChatModel;

    public PrivateChatPresenter(PrivateChatActivityInterface privateChatActivity) {
        this.privateChatActivity = privateChatActivity;
        privateChatModel = new PrivateChatModel();
    }

    @Override
    public void init(String userName) {
        privateChatModel.getMsgs(userName, new OnGetMsgsCompletedListener() {
            @Override
            public void onCompleted(List<EMMessage> messages) {
                privateChatActivity.init(messages);
            }

            @Override
            public void onError() {

            }
        });
    }

    @Override
    public void receiveMsgs(String userName) {
        privateChatModel.getMsgs(userName, new OnGetMsgsCompletedListener() {
            @Override
            public void onCompleted(List<EMMessage> messages) {
                privateChatActivity.refreshList(messages);
            }

            @Override
            public void onError() {

            }
        });
    }

    @Override
    public void getMoreMsgs(String userName) {
        privateChatModel.getMoreMsgs(userName, new OnGetMsgsCompletedListener() {
            @Override
            public void onCompleted(List<EMMessage> messages) {
                privateChatActivity.refreshMore(messages);
            }

            @Override
            public void onError() {

            }
        });
    }

    @Override
    public void clearUnreadMsgs(String userName) {
        privateChatModel.clearUnreadMsgs(userName);
    }

    @Override
    public void clearMsgs(String userName) {
        privateChatModel.clearMsgs(userName, new OnGetMsgsCompletedListener() {
            @Override
            public void onCompleted(List<EMMessage> messages) {
                privateChatActivity.refreshList(messages);
            }

            @Override
            public void onError() {

            }
        });

    }

    @Override
    public void sendText(final String userName, final String content, boolean isGroup) {
        privateChatModel.sendText(userName, content, isGroup, new EMCallBack() {
            @Override
            public void onSuccess() {

            }

            @Override
            public void onError(int i, String s) {

            }

            @Override
            public void onProgress(int i, String s) {

            }
        });
        privateChatModel.getMsgs(userName, new OnGetMsgsCompletedListener() {
            @Override
            public void onCompleted(List<EMMessage> messages) {
                privateChatActivity.refreshList(messages);
                EventBus.getDefault().post(new MsgEty(EMChatManager.getInstance().getCurrentUser()
                        , userName, content, System.currentTimeMillis(), 0));
            }

            @Override
            public void onError() {

            }
        });
    }


    @Override
    public void sendPicture(final String userName, final String fileName, final boolean isGroup) {
        privateChatModel.sendPicture(userName, fileName, isGroup, new EMCallBack() {
            @Override
            public void onSuccess() {

            }

            @Override
            public void onError(int i, String s) {

            }

            @Override
            public void onProgress(int i, String s) {

            }
        });
        privateChatModel.getMsgs(userName, new OnGetMsgsCompletedListener() {
            @Override
            public void onCompleted(List<EMMessage> messages) {
                privateChatActivity.refreshList(messages);
                EventBus.getDefault().post(new MsgEty(EMChatManager.getInstance().getCurrentUser()
                        , userName, "[图片]", System.currentTimeMillis(), 0));
            }

            @Override
            public void onError() {

            }
        });


    }

    @Override
    public void sendVoice(final String userName, String fileName, int len, boolean isGroup) {
        privateChatModel.sendVoice(userName, fileName, len, isGroup, new EMCallBack() {
            @Override
            public void onSuccess() {
            }

            @Override
            public void onError(int i, String s) {

            }

            @Override
            public void onProgress(int i, String s) {

            }
        });
        privateChatModel.getMsgs(userName, new OnGetMsgsCompletedListener() {
            @Override
            public void onCompleted(List<EMMessage> messages) {
                privateChatActivity.refreshList(messages);
                EventBus.getDefault().post(new MsgEty(EMChatManager.getInstance().getCurrentUser()
                        , userName, "[语音]", System.currentTimeMillis(), 0));
            }

            @Override
            public void onError() {

            }
        });


    }

}
